package com.shanghaionstar.dealersorder;

import android.Manifest;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.onstar.cn.common.util.MyPost;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.mvp.base.core.PermissionCall;
import com.onstar.cn.mag.dealer.MaintenanceDealerResponseEntity;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.dealersorder.adapter.DealersOrderAdapter;
import com.shanghaionstar.dealersorder.bean.DealerOrderList;
import com.shanghaionstar.dealersorder.presenter.DealersOrderPresenter;
import com.shanghaionstar.dealersorder.view.DealersOrderView;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.MyLocationUtil;
import com.shanghaionstar.weight.CommonNoDatasLayout;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;

/**
 * 预约经销商页面
 * Created by Yonglang Hong on 2016/8/11.
 */
@ContentView(R.layout.onstar_dealer_list)
public class DealersOrderActivity extends SwipeActivity implements DealersOrderView {

    @FindViewById
    TopLayout top_layout;
    @FindViewById
    ListView lsv_dealers;
    @FindViewById
    CommonNoDatasLayout cndl_noDatas;
    @FindViewById
    ProgressLayout pl_loading;

    MyLocationUtil myLoationUtil = null;

    DealersOrderAdapter adapter;

    DealersOrderPresenter dealersPresenter;

    public DealersOrderPresenter getDealersPresenter() {
        if (dealersPresenter == null) {
            dealersPresenter = new DealersOrderPresenter(getScheduler(), this);
            dealersPresenter.setOrderView(this);
        }
        return dealersPresenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        top_layout.setOnRightTextClickLstener(getResources().getString(R.string.dealer_record), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                BusinessUtil.sendReportByClickType(DealersOrderActivity.this, FunctionNameFindId.Dealer_History.getStringValue());
//                DealerB/ dealerBookingRecordFragment = new DealerBookingRecordFragment();
//                paretnActivity.changeFragment(dealerBookingRecordFragment, DealerBookingRecordFragment.class, true);
                startActivity(new Intent(DealersOrderActivity.this, OrderRecordActivity.class));
            }
        });
        top_layout.setTextColor(ContextCompat.getColor(this, R.color.right_text_color));
        loadDealersSafety();
    }

    private void loadDealersSafety() { //获取本地保存的用户位置经纬度信息
        String latitude = PreferencesUtls.getInstance(this).getString(FinalConstants.SharedPreferencesConfig.USER_CURRENT_LOCATION_LATITUDE);
        String longtitude = PreferencesUtls.getInstance(this).getString(FinalConstants.SharedPreferencesConfig.USER_CURRENT_LOCATION_LONGITUDE);
        if (!TextUtils.isEmpty(latitude) && !TextUtils.isEmpty(longtitude))
            loadDealers(latitude, longtitude);
        else
            getLocation();
    }

    private void loadDealers(String latitude, String longtitude) {
        showLoading();
        getDealersPresenter().loadDealers(latitude, longtitude);
    }

    private void showLoading() {
        pl_loading.setVisibility(View.VISIBLE);
        lsv_dealers.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.GONE);
    }

    private void getLocation() {
        requestRunTimePermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, new PermissionCall() {
            @Override
            public void requestSuccess() {
                MyLocationUtil.LocationResult locationResult = new MyLocationUtil.LocationResult() {
                    @Override
                    public void gotLocation(final Location location) {
                        if (location == null) {
                            MyPost.post(new Runnable() {
                                @Override
                                public void run() {
                                    pl_loading.setVisibility(View.GONE);
                                    lsv_dealers.setVisibility(View.GONE);
                                    cndl_noDatas.setVisibility(View.VISIBLE);
                                    cndl_noDatas.setHintText(R.string.DR_ERR004);
                                }
                            });
                        } else { //保存用户位置的经纬度
                            PreferencesUtls.getInstance(DealersOrderActivity.this).putString(FinalConstants.SharedPreferencesConfig.USER_CURRENT_LOCATION_LATITUDE, location.getLatitude() + "");
                            PreferencesUtls.getInstance(DealersOrderActivity.this).putString(FinalConstants.SharedPreferencesConfig.USER_CURRENT_LOCATION_LONGITUDE, location.getLongitude() + "");
                            MyPost.post(new Runnable() {
                                @Override
                                public void run() {
                                    loadDealers(location.getLatitude() + "", location.getLongitude() + "");
                                }
                            });
                        }
                    }
                };
                if (myLoationUtil == null)
                    try {
                        myLoationUtil = new MyLocationUtil(DealersOrderActivity.this, locationResult);
                    } catch (Exception e) {
                        e.printStackTrace();
                        myLoationUtil = null;
                    }
                if (myLoationUtil != null)
                    myLoationUtil.initMyLocation();
            }

            @Override
            public void refused() {

            }
        });

    }

    @Override
    public void showDealersList(DealerOrderList orderList) {
        pl_loading.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.GONE);
        lsv_dealers.setVisibility(View.VISIBLE);
        setAdapter(orderList.getDealers());
    }

    private void setAdapter(List<MaintenanceDealerResponseEntity> dealers) {
        if (adapter == null) {
            adapter = new DealersOrderAdapter(dealers, this);
            lsv_dealers.setAdapter(adapter);
        }
        adapter.setDealerBookingLists(dealers);
    }

    @Override
    public void showEmpty() {
        pl_loading.setVisibility(View.GONE);
        lsv_dealers.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.VISIBLE);
        cndl_noDatas.setHintText(R.string.DR_ERR005);
    }

    @Override
    public void showErr(Throwable throwable) {
        pl_loading.setVisibility(View.GONE);
        lsv_dealers.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.VISIBLE);
        cndl_noDatas.showIcon(true);
        cndl_noDatas.setHintText(R.string.no_set_meal_data_error);
        cndl_noDatas.setOnRefreshListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadDealersSafety();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dealersPresenter != null) {
            dealersPresenter.finish();
            dealersPresenter = null;
        }
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }
}
